import {
  Body,
  Controller,
  Delete,
  Get,
  HttpException,
  HttpStatus,
  Param,
  Post,
} from '@nestjs/common';
import { FriendshipService } from './friendship.service';
import { RequireLogin, UserInfo } from 'src/custom.decorator';
import { FriendAddDto } from './dto/friend-add.dto';
import { ApiOperation, ApiParam } from '@nestjs/swagger';

@Controller('friendship')
export class FriendshipController {
  constructor(private readonly friendshipService: FriendshipService) {}

  @ApiOperation({
    summary: '添加好友',
  })
  @Post('add')
  @RequireLogin()
  add(@Body() friendAddDto: FriendAddDto, @UserInfo('userId') userId: number) {
    return this.friendshipService.add(friendAddDto, userId);
  }

  @Get('request/list')
  @RequireLogin()
  list(@UserInfo('userId') userId: number) {
    return this.friendshipService.list(userId);
  }

  @Get(`agree/:friendId`)
  @RequireLogin()
  async agree(
    @Param('friendId') friendId: number,
    @UserInfo('userId') userId: number,
  ) {
    if (!friendId) {
      throw new HttpException(`friendId不能为空`, HttpStatus.BAD_REQUEST);
    }
    return await this.friendshipService.agree(friendId, userId);
  }

  @ApiOperation({
    summary: '拒绝好友请求',
  })
  @ApiParam({
    name: 'id',
    description: '好友id',
  })
  @Get('reject/:id')
  async reject(
    @Param('id') friendId: number,
    @UserInfo('userId') userId: number,
  ) {
    if (!friendId) {
      throw new HttpException('添加的好友 id 不能为空', HttpStatus.BAD_REQUEST);
    }
    return this.friendshipService.reject(friendId, userId);
  }

  @Get('list')
  @RequireLogin()
  async friendship(@UserInfo('userId') userId: number) {
    return await this.friendshipService.getFriendship(userId);
  }

  @Delete('remove/:id')
  @RequireLogin()
  async remove(
    @Param('id') friendId: number,
    @UserInfo('userId') userId: number,
  ) {
    return this.friendshipService.remove(friendId, userId);
  }
}
